function [posjieguo,veljieguo,clockjieguo]= error_analysis(obj,coordinate,myposvel,chousenum)
%UNTITLED 用于动态情况误差分析
%   此处显示详细说明

% myposvel = {jieguo}';
groundtruth = lla2ecef(obj.trajectorys.PT...
            {:, "Lat-Lon-Alt"});
groundtruth_vel = obj.trajectorys.vel;
groundtruth_clock = obj.receiver_clock_drift';
n = length(myposvel);
epoch_vector = 1:n;
REMS = zeros(n,3);
REMS_vel = zeros(n,3);
REMS_clock = zeros(n,1);

% 钟漂分析
for i = epoch_vector
    myresult = cell2mat(myposvel(i));
    true_trj_ecef = groundtruth(i,:);
    REMS(i,:) = sqrt((myresult(1:3)-true_trj_ecef)).^2;
    true_vel_ecef = enu2xyz_for_vel(groundtruth_vel(i,:),true_trj_ecef)';
    REMS_vel(i,:) = sqrt((myresult(4:6)-true_vel_ecef).^2);
    REMS_clock(i) = sqrt((myresult(7)/obj.f_obs-groundtruth_clock(i)).^2);
end
clockjieguo = mean(REMS_clock);
disp(['钟漂REMS:',num2str(clockjieguo)]);
if chousenum == 1
    figure
    plot(epoch_vector,REMS_clock);
    plot_beautier;
    xlabel('epoch','FontWeight','bold','FontName','Times New Roman');
    ylabel('s/s','FontWeight','bold','FontName','Times New Roman');
    legend('RMSE clock drift');
end
%速度误差分析，我的解算结果是ecef下，groundtruth_vel是enu下的
%因此分析的时候要转换一下
% sensor_vel_array = enu2xyz_for_vel(trj_vel(i,:),trj_ecef(i,:))';
% chose_array = [0,-1,0;1,0,0;0,0,1];%不知啥原因真是速度从enu转到ecef下坐标转了一下
%怀疑严老师轨迹生成代码是end下的，但是画图画的是enu下的，不好考证，故人为修正一下
%找到原因了，将groundtruth转了两遍lla。以后注意):
if coordinate == "ecef"
    for i = epoch_vector
        myresult = cell2mat(myposvel(i));
        true_trj_ecef = groundtruth(i,:);
        REMS(i,:) = sqrt((myresult(1:3)-true_trj_ecef)).^2;
        true_vel_ecef = enu2xyz_for_vel(groundtruth_vel(i,:),true_trj_ecef)';
        REMS_vel(i,:) = sqrt((myresult(4:6)-true_vel_ecef).^2);
    end
    posjieguo = mean(REMS);
    veljieguo = mean(REMS_vel);
    disp(['平均X向REMS：',num2str(mean(REMS(:,1)))]);
    disp(['平均Y向REMS：',num2str(mean(REMS(:,2)))]);
    disp(['平均Z向REMS：',num2str(mean(REMS(:,3)))]);
    disp(['平均VX向REMS：',num2str(mean(REMS_vel(:,1)))]);
    disp(['平均VY向REMS：',num2str(mean(REMS_vel(:,2)))]);
    disp(['平均VZ向REMS：',num2str(mean(REMS_vel(:,3)))]);
    if chousenum == 0
        return
    end
    figure('WindowState', 'maximized');
    subplot(3,1,1)
    plot(epoch_vector,REMS(:,1));
    plot_beautier;
    xlabel('epoch','FontWeight','bold','FontName','Times New Roman');
    ylabel('m','FontWeight','bold','FontName','Times New Roman');
    legend('RMSE X');
    subplot(3,1,2)
    plot(epoch_vector,REMS(:,2));
    xlabel('epoch','FontWeight','bold','FontName','Times New Roman');
    ylabel('m','FontWeight','bold','FontName','Times New Roman');
    legend('RMSE Y');
    plot_beautier;
    subplot(3,1,3)
    plot(epoch_vector,REMS(:,3));
    xlabel('epoch','FontWeight','bold','FontName','Times New Roman');
    ylabel('m','FontWeight','bold','FontName','Times New Roman');
    legend('RMSE Z');
    plot_beautier;

    figure('WindowState', 'maximized');
    subplot(3,1,1)
    plot(epoch_vector,REMS_vel(:,1));
    plot_beautier;
    xlabel('epoch','FontWeight','bold','FontName','Times New Roman');
    ylabel('m/s','FontWeight','bold','FontName','Times New Roman');
    legend('RMSE VX');
    subplot(3,1,2)
    plot(epoch_vector,REMS_vel(:,2));
    xlabel('epoch','FontWeight','bold','FontName','Times New Roman');
    ylabel('m/s','FontWeight','bold','FontName','Times New Roman');
    legend('RMSE VY');
    plot_beautier;
    subplot(3,1,3)
    plot(epoch_vector,REMS_vel(:,3));
    xlabel('epoch','FontWeight','bold','FontName','Times New Roman');
    ylabel('m/s','FontWeight','bold','FontName','Times New Roman');
    legend('RMSE VZ');
    plot_beautier;
    
    posjieguo = mean(REMS);
    veljieguo = mean(REMS_vel);
elseif coordinate == "ned"
    gt = ecef2lla(groundtruth);
    wgs84 = wgs84Ellipsoid('meter');
    for i = epoch_vector
        true_vel_ned = groundtruth_vel(i,:)*[0,1,0;1,0,0;0,0,-1];
        myresult = cell2mat(myposvel(i));
        my_vel_ned = xyz2enu_for_vel(myresult(4:6),myresult(1:3))'*[0,1,0;1,0,0;0,0,-1];
        [nr,er,dr] = ecef2ned(myresult(1),myresult(2),myresult(3),gt(i,1),gt(i,2),gt(i,3)...
            ,wgs84);
        REMS(i,:) = sqrt([nr,er,dr].^2);
        REMS_vel(i,:) = sqrt((my_vel_ned-true_vel_ned).^2);
    end

    posjieguo = mean(REMS);
    veljieguo = mean(REMS_vel);
     disp(['N向REMS：',num2str(mean(REMS(:,1)))]);
    disp(['E向REMS：',num2str(mean(REMS(:,2)))]);
    disp(['D向REMS：',num2str(mean(REMS(:,3)))]);
    disp(['VN向REMS：',num2str(mean(REMS_vel(:,1)))]);
    disp(['VE向REMS：',num2str(mean(REMS_vel(:,2)))]);
    disp(['VD向REMS：',num2str(mean(REMS_vel(:,3)))]);
    if chousenum == 0
        return
    end
    figure('WindowState', 'maximized');
    subplot(3,1,1)
    plot(epoch_vector,REMS(:,1),'LineWidth', 2);
    plot_beautier;
    set(gca, 'FontSize', 25) 
    xlabel('epoch','FontWeight','bold','FontName','Times New Roman','FontSize', 25);
    ylabel('m','FontWeight','bold','FontName','Times New Roman','FontSize', 25);
    legend('RMSE N');
    subplot(3,1,2)
    set(gca, 'FontSize', 25)
    plot(epoch_vector,REMS(:,2),'LineWidth', 2);
    set(gca, 'FontSize', 25)
    xlabel('epoch','FontWeight','bold','FontName','Times New Roman','FontSize', 25);
    ylabel('m','FontWeight','bold','FontName','Times New Roman','FontSize', 25);
    legend('RMSE E');
    plot_beautier;
    set(gca, 'FontSize', 25)
    subplot(3,1,3)
    set(gca, 'FontSize', 25)
    plot(epoch_vector,REMS(:,3),'LineWidth', 2);
    set(gca, 'FontSize', 25)
    xlabel('epoch','FontWeight','bold','FontName','Times New Roman','FontSize', 25);
    ylabel('m','FontWeight','bold','FontName','Times New Roman','FontSize', 25);
    legend('RMSE D');
    plot_beautier;
    set(gca, 'FontSize', 25)
    figure('WindowState', 'maximized');
    subplot(3,1,1)
    
    plot(epoch_vector,REMS_vel(:,1),'LineWidth', 2);
    set(gca, 'FontSize', 25)
    plot_beautier;
    set(gca, 'FontSize', 25)
    xlabel('epoch','FontWeight','bold','FontName','Times New Roman','FontSize', 25);
    ylabel('m/s','FontWeight','bold','FontName','Times New Roman','FontSize', 25);
    legend('RMSE VN');
    subplot(3,1,2)
    set(gca, 'FontSize', 25)
    plot(epoch_vector,REMS_vel(:,2),'LineWidth', 2);
    set(gca, 'FontSize', 25)
    xlabel('epoch','FontWeight','bold','FontName','Times New Roman','FontSize', 25);
    ylabel('m/s','FontWeight','bold','FontName','Times New Roman','FontSize', 25);
    legend('RMSE VE');
    plot_beautier;
    set(gca, 'FontSize', 25)
    subplot(3,1,3)
    set(gca, 'FontSize', 25)
    plot(epoch_vector,REMS_vel(:,3),'LineWidth', 2);
    set(gca, 'FontSize', 25)
    xlabel('epoch','FontWeight','bold','FontName','Times New Roman','FontSize', 25);
    ylabel('m/s','FontWeight','bold','FontName','Times New Roman','FontSize', 25);
    legend('RMSE VD');
    plot_beautier;
    set(gca, 'FontSize', 25)

   

end
figure('WindowState', 'maximized');
DGDOP = cell2mat({obj.DDOP.DGDOP});
subplot(2,1,1)
plot(epoch_vector,DGDOP);
xlabel('epoch','FontWeight','bold','FontName','Times New Roman');
ylabel('DGDOP','FontWeight','bold','FontName','Times New Roman');
legend('DGDOP');
plot_beautier;
visible_num = [];
temp = {obj.sat_obs.satobs};
for i = 1:length({obj.sat_obs.satobs})
    visible_num(i) = length(cell2mat(temp(i)));
end
subplot(2,1,2)
plot(epoch_vector,visible_num);
xlabel('epoch','FontWeight','bold','FontName','Times New Roman');
ylabel('number','FontWeight','bold','FontName','Times New Roman');
legend('num of visible sat');
plot_beautier;
disp("误差分析结束");
end

